<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .out{
            width: 400px;
            height: 200px;
            border: 1px solid red;
        }
        .box{
            height: 100px;
            width: 100px;
            background-color: bisque;
            border-radius: 50%;
        }
    </style>
</head>
<body>
    <div class="out">
        <div class="box" style="margin-left: 0;"></div>
    </div>
    
</body>
<script>
    let a=0;
    function boxCallback(){
        let boxObj=document.querySelector('.box');
        let toLeft=parseInt(boxObj.style.marginLeft);
        if(a==0){
            if(toLeft<=300){
                boxObj.style.marginLeft=(toLeft+1)+'px';
            }
            if(toLeft>=300){
                a=1;
            }
            console.log(a)
            
        }else if(a==1){
            if(toLeft>=0){
                boxObj.style.marginLeft=(toLeft-1)+'px';
            }
            if(toLeft<=0){
                a=0;
            }
            
        }
    
        requestAnimationFrame(boxCallback);

        
    }
    window.requestAnimationFrame(boxCallback)
</script>
</html>